package net.dopan.pigframe.oss.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Package: net.dopan.pigframe.oss.utils
 * @Title: MimeTypeFileExtensionConvert
 * @Description: 文件MIME检测
 * @author: 刘宽
 * @date: 2018-08-22 12:41
 */
public class MimeTypeFileExtensionConvertUtils {

    private static Map<String, String> maps = new HashMap<>();

    static {
        maps.put(".mp3", "audio/mpeg3");
        maps.put(".avi", "video/avi");
        maps.put(".wav", "audio/wav");
        maps.put(".jpeg", "image/jpeg");
        maps.put(".jpe", "image/jpeg");
        maps.put(".jpg", "image/jpeg");
        maps.put(".gif", "image/gif");
        maps.put(".png", "image/png");
        maps.put(".bmp", "image/bmp");
        maps.put(".html", "text/html");
        maps.put(".htm", "text/html");
        maps.put(".xml", "text/xml");
        maps.put(".txt", "text/plain");
        maps.put(".vsd", "application/x-vsd");
        maps.put(".ppt", "application/vnd.ms-powerpoint");
        maps.put(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        maps.put(".pps", "application/vnd.ms-powerpoint");
        maps.put(".ppa", "application/vnd.ms-powerpoint");
        maps.put(".doc", "aapplication/msword");
        maps.put(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        maps.put(".rtf", "aapplication/msword");
        maps.put(".dot", "aapplication/msword");
        maps.put(".xls", "application/vnd.ms-excel");
        maps.put(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        maps.put(".pdf", "application/pdf");
        maps.put(".svg", "application/x-shockwave-flash");
        maps.put(".exe", "application/octet-stream ");
        maps.put(".rar", "application/x-rar");
        maps.put(".zip", "application/zip");
        maps.put(".gzip", "application/x-gzip");
        maps.put(".gz", "application/x-gzip");
    }

    public static String getFileExtension(String fileName) {
        String mimeType = fileName.substring(fileName.lastIndexOf("."));
        String mime = maps.get(mimeType);
        if (mime == null) {
            return "unknown";
        } else {
            return mime;
        }
    }

}